package com.ruoyi.web.controller.prt;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.prt.domain.PrtCoupon;
import com.ruoyi.prt.domain.vo.PrtCouponVO;
import com.ruoyi.prt.domain.vo.PrtRechargeLogVO;
import com.ruoyi.prt.service.IPrtCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券Controller
 *
 * @author xiaobo Yan
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/prt/coupon")
public class PrtCouponController extends BaseController
{
    @Autowired
    private IPrtCouponService prtCouponService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrtCoupon prtCoupon)
    {
        startPage();
        List<PrtCoupon> list = prtCouponService.selectPrtCouponList(prtCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PrtCoupon prtCoupon)
    {
        List<PrtCoupon> list = prtCouponService.selectPrtCouponList(prtCoupon);
        ExcelUtil<PrtCoupon> util = new ExcelUtil<PrtCoupon>(PrtCoupon.class);
        return util.exportExcel(list, "coupon");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") String couponId)
    {
        return AjaxResult.success(prtCouponService.selectPrtCouponById(couponId));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrtCoupon prtCoupon)
    {
        return toAjax(prtCouponService.insertPrtCoupon(prtCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrtCoupon prtCoupon)
    {
        return toAjax(prtCouponService.updatePrtCoupon(prtCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable String[] couponIds)
    {
        return toAjax(prtCouponService.deletePrtCouponByIds(couponIds));
    }

    /**
     * 按用户id查询优惠券列表
     */
    @GetMapping("/getConponList")
    @Log(title = "按用户id查询优惠券列表", businessType = BusinessType.SELECT)
    @PreAuthorize("@ss.hasRole('mobile')")
    public AjaxResult getConponList(PrtCouponVO prtCouponVO)
    {
        prtCouponVO.setRecipient(SecurityUtils.getUserId());
        return AjaxResult.success(prtCouponService.selectPrtCouponList(prtCouponVO));
    }
}
